package cn.iocoder.yudao.module.call.controller.admin.aiqualitytesting;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.call.controller.admin.aiqualitytesting.vo.AiQualityTestingPageReqVO;
import cn.iocoder.yudao.module.call.controller.admin.aiqualitytesting.vo.AiQualityTestingRespVO;
import cn.iocoder.yudao.module.call.controller.admin.aiqualitytesting.vo.AiQualityTestingSaveReqVO;
import cn.iocoder.yudao.module.call.dal.dataobject.aiqualitytesting.AiQualityTestingDO;
import cn.iocoder.yudao.module.call.dal.dataobject.record.CallRecordDO;
import cn.iocoder.yudao.module.call.service.aiqualitytesting.AiQualityTestingService;
import cn.iocoder.yudao.module.call.service.ivrintentionlabelstatistics.IvrIntentionLabelStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 大模型质检")
@RestController
@RequestMapping("/call/ai-quality-testing")
@Validated
public class AiQualityTestingController {

    @Resource
    private AiQualityTestingService aiQualityTestingService;

    @PostMapping("/startAiQualityTesting")
    @Operation(summary = "创建大模型质检")
    public CommonResult<String> startAiQualityTesting(@Valid @RequestBody AiQualityTestingDO createReqVO) {
        CallRecordDO callRecordDO = new CallRecordDO();
        callRecordDO.setReqNo(createReqVO.getReqNo());
        callRecordDO.setId(createReqVO.getCallRecordId());
        callRecordDO.setCallee(createReqVO.getCallee());
        aiQualityTestingService.startAiQualityTesting(createReqVO.getOwnerUserId(), createReqVO.getTenantId(), callRecordDO, createReqVO);
        return success("调用成功");
    }

    @PostMapping("/create")
    @Operation(summary = "创建大模型质检")
    @PreAuthorize("@ss.hasPermission('call:task:create')")
    public CommonResult<Long> createAiQualityTesting(@Valid @RequestBody AiQualityTestingSaveReqVO createReqVO) {
        return success(aiQualityTestingService.createAiQualityTesting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新大模型质检")
    @PreAuthorize("@ss.hasPermission('call:task:update')")
    public CommonResult<Boolean> updateAiQualityTesting(@Valid @RequestBody AiQualityTestingSaveReqVO updateReqVO) {
        aiQualityTestingService.updateAiQualityTesting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除大模型质检")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:task:delete')")
    public CommonResult<Boolean> deleteAiQualityTesting(@RequestParam("id") Long id) {
        aiQualityTestingService.deleteAiQualityTesting(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得大模型质检")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:task:query')")
    public CommonResult<AiQualityTestingRespVO> getAiQualityTesting(@RequestParam("id") Long id) {
        AiQualityTestingDO aiQualityTesting = aiQualityTestingService.getAiQualityTesting(id);
        return success(BeanUtils.toBean(aiQualityTesting, AiQualityTestingRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得大模型质检分页")
    @PreAuthorize("@ss.hasPermission('call:task:query')")
    public CommonResult<List<AiQualityTestingRespVO>> simpleList() {
        List<AiQualityTestingDO> aiQualityTestingDOS = aiQualityTestingService.simpleList();
        return success(BeanUtils.toBean(aiQualityTestingDOS, AiQualityTestingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得大模型质检分页")
    @PreAuthorize("@ss.hasPermission('call:task:query')")
    public CommonResult<PageResult<AiQualityTestingRespVO>> getAiQualityTestingPage(@Valid AiQualityTestingPageReqVO pageReqVO) {
        PageResult<AiQualityTestingDO> pageResult = aiQualityTestingService.getAiQualityTestingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AiQualityTestingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出大模型质检 Excel")
    @PreAuthorize("@ss.hasPermission('call:task:export')")
    @OperateLog(type = EXPORT)
    public void exportAiQualityTestingExcel(@Valid AiQualityTestingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AiQualityTestingDO> list = aiQualityTestingService.getAiQualityTestingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "大模型质检.xls", "数据", AiQualityTestingRespVO.class,
                        BeanUtils.toBean(list, AiQualityTestingRespVO.class));
    }

}